package com.homedo.microservice.open.service.gateway.alert.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.enums.forzuulfilterdefault.ProgressEnum4ZuulFilterDefault;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.enums.forzuulfilterdefault.StatusEnum4ZuulFilterDefault;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.po.ZuulFilterDefault;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.po.enhanced.ConfigEnhanced;
import com.homedo.microservice.open.service.gateway.alert.gateway.bean.po.enhanced.ZuulFilterDefaultEnhanced;
import com.homedo.microservice.open.service.gateway.alert.gateway.persistence.dao.ZuulFilterDefaultDao;
import com.homedo.microservice.open.service.gateway.alert.gateway.support.ApplicationNameHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月14日 14时43分20秒
 */
@Service
public class ZuulFilterDefaultService extends BaseService<ZuulFilterDefault> {
    @Autowired
    private ZuulFilterDefaultDao zuulFilterDefaultDao;
    @Autowired
    private ConfigService configService;

    @Override
    public List<ZuulFilterDefaultEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<ZuulFilterDefault> zuulFilterDefaults = zuulFilterDefaultDao.getListByIds(ids, ZuulFilterDefault.class);

        if (!Detect.notEmpty(zuulFilterDefaults)) {
            return null;
        }

        List<ZuulFilterDefaultEnhanced> zuulFilterEnhanceds = this.convent2Enhanceds(zuulFilterDefaults);
        this.appendZuulFilterDisableConfEnhanced(zuulFilterEnhanceds, userContext);

        return zuulFilterEnhanceds;
    }


    @Override
    public List<ZuulFilterDefaultEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<ZuulFilterDefault> zuulFilterDefaults = zuulFilterDefaultDao.getListByIdsGreaterThanMark0(ids, ZuulFilterDefault.class);

        if (!Detect.notEmpty(zuulFilterDefaults)) {
            return null;
        }

        List<ZuulFilterDefaultEnhanced> zuulFilterEnhanceds = this.convent2Enhanceds(zuulFilterDefaults);
        this.appendZuulFilterDisableConfEnhanced(zuulFilterEnhanceds, userContext);

        return zuulFilterEnhanceds;
    }


    @Override
    public List<ZuulFilterDefaultEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<ZuulFilterDefault> zuulFilterDefaults = (List<ZuulFilterDefault>) pos;
        List<ZuulFilterDefaultEnhanced> zuulFilterDefaultEnhanceds = Lists.newArrayList();
        zuulFilterDefaults.forEach(e -> zuulFilterDefaultEnhanceds.add(new ZuulFilterDefaultEnhanced(e)));
        return zuulFilterDefaultEnhanceds;
    }

    public List<ZuulFilterDefaultEnhanced> getEnhanceds4Application(String applicationName, List<ProgressEnum4ZuulFilterDefault> progresses, List<StatusEnum4ZuulFilterDefault> statuses, UserContext userContext) {
        List<ZuulFilterDefault> zuulFilters = zuulFilterDefaultDao.getList4Application(applicationName, progresses, statuses);

        if (!Detect.notEmpty(zuulFilters)) {
            return null;
        }
        List<ZuulFilterDefaultEnhanced> zuulFilterDefaultEnhanceds = this.convent2Enhanceds(zuulFilters);
        this.appendZuulFilterDisableConfEnhanced(zuulFilterDefaultEnhanceds, userContext);

        return zuulFilterDefaultEnhanceds;
    }

    public List<ZuulFilterDefaultEnhanced> appendZuulFilterDisableConfEnhanced(List<ZuulFilterDefaultEnhanced> zuulFilterDefaultEnhanceds, UserContext userContext) {
        List<String> disablePropertyNames = zuulFilterDefaultEnhanceds.stream().map(ZuulFilterDefaultEnhanced::getDisablePropertyName).collect(Collectors.toList());

        List<ConfigEnhanced> configEnhanceds = configService.getEnhanceds4Application(ApplicationNameHolder.get(), disablePropertyNames, userContext);

        if (!Detect.notEmpty(configEnhanceds)) {
            return zuulFilterDefaultEnhanceds;
        }

        this.appendZuulFilterDisableConfEnhanced(zuulFilterDefaultEnhanceds, configEnhanceds, userContext);

        return zuulFilterDefaultEnhanceds;
    }

    private void appendZuulFilterDisableConfEnhanced(List<ZuulFilterDefaultEnhanced> zuulFilterDefaultEnhanceds, List<ConfigEnhanced> configEnhanceds, UserContext userContext) {
        zuulFilterDefaultEnhanceds.forEach(e -> {
            this.appendZuulFilterDisableConfEnhanced(e, configEnhanceds, userContext);
        });
    }


    private void appendZuulFilterDisableConfEnhanced(ZuulFilterDefaultEnhanced zuulFilterEnhanced, List<ConfigEnhanced> configEnhanceds, UserContext userContext) {
        configEnhanceds.forEach(e -> {
            if (e.getApplicationName().equals(zuulFilterEnhanced.getApplicationName()) && e.getName().equals(zuulFilterEnhanced.getDisablePropertyName())) {
                zuulFilterEnhanced.setZuulFilterDisableConfEnhanced(e);
                return;
            }
        });
    }


}
